package com.mlizhi.modules.spec.record;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.mlizhi.base.MlzApplication;
import com.mlizhi.base.Session;
import com.mlizhi.kingdom.R;
import com.mlizhi.modules.spec.ISpecInterface;
import com.mlizhi.modules.spec.SpecActivity;
import com.mlizhi.modules.spec.dao.DaoSession;
import com.mlizhi.modules.spec.dao.DetectDao;
import com.mlizhi.modules.spec.dao.model.DetectModel;
import com.mlizhi.modules.spec.record.adapter.RecordPartAdapter;
import com.mlizhi.modules.spec.util.DateFormatUtil;
import com.mlizhi.utils.Constants;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class SpecRecordFragment extends Fragment {
    // 缓存Fragment view
    private View rootView;
    // ISpecInterface的实例，用来使SpecActivity做回调的
    private ISpecInterface specCallback;
    private Context mContext;
    private static SpecRecordFragment specRecordFragment;
    private Session mSession;
    /**
     * 标志button，用来确定点击了日、周、月的
     */
    private int timeType = Constants.BODY_PART_CURRENT_DAY;

    private ListView recordListView;

    private List<HashMap<String, String>> recordPartList;

    private RecordPartAdapter recordPartAdapter;

    private RadioGroup recordPartGroup;

    private DaoSession daoSession;
    private DetectDao detectDao;
    private String userId;
    private MlzApplication mlzApplication;
    private Date currentDate = null;

    public SpecRecordFragment() {

    }

    public static SpecRecordFragment getNewInstance() {
        if (specRecordFragment == null) {
            specRecordFragment = new SpecRecordFragment();
        }
        return specRecordFragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext = activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mlzApplication = (MlzApplication) getActivity().getApplication();
        mSession = Session.get(getActivity().getApplicationContext());
        daoSession = mlzApplication.getDaoSession();
        detectDao = daoSession.getDetectDao();
        userId = mSession.getUid();
        if (userId == null || "".equals(userId)) {
            userId = mSession.getMac();
        }
        currentDate = DateFormatUtil.formatDate(new Date());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_spec_record, container, false);
        }
        // 缓存的rootView需要判断是否已经被加过parent，
        // 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        recordListView = (ListView) rootView.findViewById(R.id.id_part_category_list);
        recordPartGroup = (RadioGroup) rootView.findViewById(R.id.id_id_btn4time_group);
        recordPartGroup.check(R.id.id_btn4day);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        specCallback = (ISpecInterface) getActivity();

        initView();
        recordPartGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.id_btn4day:
                        initDayView();
                        timeType = Constants.BODY_PART_CURRENT_DAY;
                        MobclickAgent.onEvent(mContext, "selectHistoryDataByDay");
                        recordPartGroup.check(R.id.id_btn4day);
                        break;
                    case R.id.id_btn4week:
                        initWeekView();
                        timeType = Constants.BODY_PART_CURRENT_WEEK;
                        MobclickAgent.onEvent(mContext, "selectHistoryDataByWeek");
                        recordPartGroup.check(R.id.id_btn4week);
                        break;
                    case R.id.id_btn4month:
                        initMonthView();
                        timeType = Constants.BODY_PART_CURRENT_MONTH;
                        MobclickAgent.onEvent(mContext, "selectHistoryDataByMonth");
                        recordPartGroup.check(R.id.id_btn4month);
                        break;
                    default:
                        break;
                }
            }
        });


        recordListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                HashMap<String, String> typeMap = recordPartList.get((int) id);
                String waterValue = typeMap.get(Constants.BODY_PART_WATER_VALUE);
                float waterFloat = 0.0f;
                try {
                    waterFloat = Float.parseFloat(waterValue);
                } catch (Exception e) {
                    waterFloat = 0.0f;
                }
                if (timeType == Constants.BODY_PART_CURRENT_DAY) {
                    if (waterFloat != 0.0f) {
                        startActivity(typeMap, timeType);
                    } else {
                        dialog(mContext.getResources().getString(R.string.detect_report_day), typeMap, timeType);
                    }
                } else if (timeType == Constants.BODY_PART_CURRENT_WEEK) {
                    if (waterFloat != 0.0f) {
                        startActivity(typeMap, timeType);
                    } else {
                        dialog(mContext.getResources().getString(R.string.detect_report_week), typeMap, timeType);
                    }
                } else if (timeType == Constants.BODY_PART_CURRENT_MONTH) {
                    if (waterFloat != 0.0f) {
                        startActivity(typeMap, timeType);
                    } else {
                        dialog(mContext.getResources().getString(R.string.detect_report_month), typeMap, timeType);
                    }
                }
                MobclickAgent.onEvent(mContext, "go2historyView");

            }
        });
    }

    /**
     * 默认
     */
    private void initView() {
        initDayView();
    }

    /**
     * 以天为单位的
     */
    private void initDayView() {
        recordPartList = new ArrayList<HashMap<String, String>>();
        recordPartAdapter = new RecordPartAdapter(getActivity());
        Date mediumTime = DateFormatUtil.getBeforeDay(currentDate);
        Date preCurrentTime = DateFormatUtil.getBeforeDay(mediumTime);
        Date endTime = DateFormatUtil.getNextDay(currentDate);

        HashMap<String, String> faceMap = new HashMap<String, String>();
        float preDayAvgFaceValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_FACE), userId, String.valueOf(preCurrentTime.getTime()), String.valueOf(mediumTime.getTime()));
        float currentDayAvgFaceValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_FACE), userId, String.valueOf(mediumTime.getTime()), String.valueOf(endTime.getTime()));

        faceMap.put(Constants.BODY_PART_TYPE, String.valueOf(Constants.BODY_PART_FACE));
        faceMap.put(Constants.BODY_PART_COMPARE_LABEL, getString(R.string.skin_healthLabel4comparsion_day));
        if (preDayAvgFaceValue == 0.0f) {
            faceMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentDayAvgFaceValue - preDayAvgFaceValue) / 0.01 * 10) / 10f + "%"));
        } else {
            faceMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentDayAvgFaceValue - preDayAvgFaceValue) / preDayAvgFaceValue * 10) / 10f + "%"));
        }
//			faceMap.put(Constants.BODY_PART_COMPARE_PROCESS, "20");
        faceMap.put(Constants.BODY_PART_WATER_VALUE, String.valueOf(Math.ceil(currentDayAvgFaceValue * 1000) / 10f));
//			faceMap.put(Constants.BODY_PART_CONDITION_VALUE, getString(R.string.skin_health4normal));
        recordPartList.add(faceMap);
        HashMap<String, String> eyeMap = new HashMap<String, String>();
        float preDayAvgEyeValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_EYE), userId, String.valueOf(preCurrentTime.getTime()), String.valueOf(mediumTime.getTime()));
        float currentDayAvgEyeValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_EYE), userId, String.valueOf(mediumTime.getTime()), String.valueOf(endTime.getTime()));
        eyeMap.put(Constants.BODY_PART_TYPE, String.valueOf(Constants.BODY_PART_EYE));
        eyeMap.put(Constants.BODY_PART_COMPARE_LABEL, getString(R.string.skin_healthLabel4comparsion_day));
        if (preDayAvgEyeValue == 0.0f) {
            eyeMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentDayAvgEyeValue - preDayAvgEyeValue) / 0.01f * 10) / 10f + "%"));
        } else {
            eyeMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentDayAvgEyeValue - preDayAvgEyeValue) / preDayAvgEyeValue * 10) / 10f + "%"));
        }

//			eyeMap.put(Constants.BODY_PART_COMPARE_PROCESS, "20");
        eyeMap.put(Constants.BODY_PART_WATER_VALUE, String.valueOf(Math.ceil(currentDayAvgEyeValue * 1000) / 10f));
//			eyeMap.put(Constants.BODY_PART_CONDITION_VALUE, getString(R.string.skin_health4normal));
        recordPartList.add(eyeMap);
//			HashMap<String,String> neckMap = new HashMap<String, String>();
//			float preDayAvgNeckValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_NECK),userId,String.valueOf(preCurrentTime.getTime()),String.valueOf(mediumTime.getTime()));
//			float currentDayAvgNeckValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_NECK),userId,String.valueOf(mediumTime.getTime()),String.valueOf(endTime.getTime()));
//			neckMap.put(Constants.BODY_PART_TYPE, String.valueOf(Constants.BODY_PART_NECK));
//			neckMap.put(Constants.BODY_PART_COMPARE_LABEL,  getString(R.string.skin_healthLabel4comparsion_day));
//			if(preDayAvgNeckValue == 0.0f) {
//				neckMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentDayAvgNeckValue - preDayAvgNeckValue)/0.01f * 10)/10f + "%"));
//			}else{
//				neckMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentDayAvgNeckValue - preDayAvgNeckValue)/preDayAvgNeckValue * 10)/10f + "%"));
//			}
//			
////			neckMap.put(Constants.BODY_PART_COMPARE_PROCESS, "20");
//			neckMap.put(Constants.BODY_PART_WATER_VALUE, String.valueOf(Math.ceil(currentDayAvgNeckValue * 1000)/10f));
////			neckMap.put(Constants.BODY_PART_CONDITION_VALUE, getString(R.string.skin_health4normal));
//			recordPartList.add(neckMap);
        HashMap<String, String> handMap = new HashMap<String, String>();
        float preDayAvgHandValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_HAND), userId, String.valueOf(preCurrentTime.getTime()), String.valueOf(mediumTime.getTime()));
        float currentDayAvgHandValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_HAND), userId, String.valueOf(mediumTime.getTime()), String.valueOf(endTime.getTime()));
        handMap.put(Constants.BODY_PART_TYPE, String.valueOf(Constants.BODY_PART_HAND));
        handMap.put(Constants.BODY_PART_COMPARE_LABEL, getString(R.string.skin_healthLabel4comparsion_day));
        if (preDayAvgHandValue == 0.0f) {
            handMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentDayAvgHandValue - preDayAvgHandValue) / 0.01f * 10) / 10f + "%"));
        } else {
            handMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentDayAvgHandValue - preDayAvgHandValue) / preDayAvgHandValue * 10) / 10f + "%"));
        }
//			handMap.put(Constants.BODY_PART_COMPARE_PROCESS, "20");
        handMap.put(Constants.BODY_PART_WATER_VALUE, String.valueOf(Math.ceil(currentDayAvgHandValue * 1000) / 10f));
//			handMap.put(Constants.BODY_PART_CONDITION_VALUE, getString(R.string.skin_health4normal));
        recordPartList.add(handMap);
        recordPartAdapter.setRecordPartList(recordPartList);
        recordListView.setAdapter(recordPartAdapter);
    }

    /**
     * 以月为单位的
     */
    private void initMonthView() {
        Date preWeekStartTime = DateFormatUtil.getFirstDayOfMonth(DateFormatUtil.getBeforeMonthCurrentDate(currentDate));
        Date mediumTime = DateFormatUtil.getFirstDayOfMonth(currentDate);
        Date endTime = DateFormatUtil.getNextDay(DateFormatUtil.getLastDayOfMonth(currentDate));
        recordPartList = new ArrayList<HashMap<String, String>>();
        recordPartAdapter = new RecordPartAdapter(getActivity());
        endTime = DateFormatUtil.getNextDay(endTime);
        HashMap<String, String> faceMap = new HashMap<String, String>();
        float preMonthAvgFaceValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_FACE), userId, String.valueOf(preWeekStartTime.getTime()), String.valueOf(mediumTime.getTime()));
        float currentMonthAvgFaceValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_FACE), userId, String.valueOf(mediumTime.getTime()), String.valueOf(endTime.getTime()));
        faceMap.put(Constants.BODY_PART_TYPE, String.valueOf(Constants.BODY_PART_FACE));
        faceMap.put(Constants.BODY_PART_COMPARE_LABEL, getString(R.string.skin_healthLabel4comparsion_month));
        if (preMonthAvgFaceValue == 0.0f) {
            faceMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentMonthAvgFaceValue - preMonthAvgFaceValue) / 0.01f * 10) / 10f + "%"));
        } else {
            faceMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentMonthAvgFaceValue - preMonthAvgFaceValue) / preMonthAvgFaceValue * 10) / 10f + "%"));
        }

//			faceMap.put(Constants.BODY_PART_COMPARE_PROCESS, "20");
        faceMap.put(Constants.BODY_PART_WATER_VALUE, String.valueOf(Math.ceil(currentMonthAvgFaceValue * 1000) / 10f));
//			faceMap.put(Constants.BODY_PART_CONDITION_VALUE, getString(R.string.skin_health4normal));
        recordPartList.add(faceMap);
        HashMap<String, String> eyeMap = new HashMap<String, String>();
        float preMonthAvgEyeValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_EYE), userId, String.valueOf(preWeekStartTime.getTime()), String.valueOf(mediumTime.getTime()));
        float currentMonthAvgEyeValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_EYE), userId, String.valueOf(mediumTime.getTime()), String.valueOf(endTime.getTime()));
        eyeMap.put(Constants.BODY_PART_TYPE, String.valueOf(Constants.BODY_PART_EYE));
        eyeMap.put(Constants.BODY_PART_COMPARE_LABEL, getString(R.string.skin_healthLabel4comparsion_month));
        if (preMonthAvgEyeValue == 0.0f) {
            eyeMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentMonthAvgEyeValue - preMonthAvgEyeValue) / 0.01f * 10) / 10f + "%"));
        } else {
            eyeMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentMonthAvgEyeValue - preMonthAvgEyeValue) / preMonthAvgEyeValue * 10) / 10f + "%"));
        }

//			eyeMap.put(Constants.BODY_PART_COMPARE_PROCESS, "20");
        eyeMap.put(Constants.BODY_PART_WATER_VALUE, String.valueOf(Math.ceil(currentMonthAvgEyeValue * 1000) / 10f));
//			eyeMap.put(Constants.BODY_PART_CONDITION_VALUE, getString(R.string.skin_health4normal));
        recordPartList.add(eyeMap);
//			HashMap<String,String> neckMap = new HashMap<String, String>();
//			float preMonthAvgNeckValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_NECK),userId,String.valueOf(preWeekStartTime.getTime()),String.valueOf(mediumTime.getTime()));
//			float currentMonthAvgNeckValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_NECK),userId,String.valueOf(mediumTime.getTime()),String.valueOf(endTime.getTime()));
//			neckMap.put(Constants.BODY_PART_TYPE, String.valueOf(Constants.BODY_PART_NECK));
//			neckMap.put(Constants.BODY_PART_COMPARE_LABEL,  getString(R.string.skin_healthLabel4comparsion_month));
////			neckMap.put(Constants.BODY_PART_COMPARE_PROCESS, "20");
//			if(preMonthAvgNeckValue == 0.0f){
//				neckMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentMonthAvgNeckValue - preMonthAvgNeckValue)/0.01f * 10)/10f + "%"));
//			}else{
//				neckMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentMonthAvgNeckValue - preMonthAvgNeckValue)/preMonthAvgNeckValue * 10)/10f + "%"));
//			}
//			neckMap.put(Constants.BODY_PART_WATER_VALUE, String.valueOf(Math.ceil(currentMonthAvgNeckValue * 1000)/10f));
////			neckMap.put(Constants.BODY_PART_CONDITION_VALUE, getString(R.string.skin_health4normal));
//			recordPartList.add(neckMap);
        HashMap<String, String> handMap = new HashMap<String, String>();
        float preMonthAvgHandValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_HAND), userId, String.valueOf(preWeekStartTime.getTime()), String.valueOf(mediumTime.getTime()));
        float currentMonthAvgHandValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_HAND), userId, String.valueOf(mediumTime.getTime()), String.valueOf(endTime.getTime()));

        handMap.put(Constants.BODY_PART_TYPE, String.valueOf(Constants.BODY_PART_HAND));
        handMap.put(Constants.BODY_PART_COMPARE_LABEL, getString(R.string.skin_healthLabel4comparsion_month));
        if (preMonthAvgHandValue == 0.0f) {
            handMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentMonthAvgHandValue - preMonthAvgHandValue) / 0.01f * 10) / 10f + "%"));
        } else {
            handMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentMonthAvgHandValue - preMonthAvgHandValue) / preMonthAvgHandValue * 10) / 10f + "%"));
        }

//			handMap.put(Constants.BODY_PART_COMPARE_PROCESS, "20");
        handMap.put(Constants.BODY_PART_WATER_VALUE, String.valueOf(Math.ceil(currentMonthAvgHandValue * 1000) / 10f));
//			handMap.put(Constants.BODY_PART_CONDITION_VALUE, getString(R.string.skin_health4normal));
        recordPartList.add(handMap);
        recordPartAdapter.setRecordPartList(recordPartList);
        recordListView.setAdapter(recordPartAdapter);
    }

    /**
     * 以周为单位的
     */
    private void initWeekView() {
        Date preMonthStartTime = DateFormatUtil.getFirstDayOfWeek(DateFormatUtil.getBeforeWeekCurrentDate(currentDate));

        Date mediumTime = DateFormatUtil.getFirstDayOfWeek(currentDate);
        Date endTime = DateFormatUtil.getNextDay(DateFormatUtil.getLastDayOfWeek(currentDate));
        recordPartList = new ArrayList<HashMap<String, String>>();
        recordPartAdapter = new RecordPartAdapter(getActivity());
        HashMap<String, String> faceMap = new HashMap<String, String>();
        float preWeekAvgFaceValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_FACE), userId, String.valueOf(preMonthStartTime.getTime()), String.valueOf(mediumTime.getTime()));
        float currentWeekAvgFaceValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_FACE), userId, String.valueOf(mediumTime.getTime()), String.valueOf(endTime.getTime()));
        faceMap.put(Constants.BODY_PART_TYPE, String.valueOf(Constants.BODY_PART_FACE));
        faceMap.put(Constants.BODY_PART_COMPARE_LABEL, getString(R.string.skin_healthLabel4comparsion_week));
        if (preWeekAvgFaceValue == 0.0f) {
            faceMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentWeekAvgFaceValue - preWeekAvgFaceValue) / 0.01f * 10) / 10f + "%"));
        } else {
            faceMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentWeekAvgFaceValue - preWeekAvgFaceValue) / preWeekAvgFaceValue * 10) / 10f + "%"));
        }

//			faceMap.put(Constants.BODY_PART_COMPARE_PROCESS, "20");
        faceMap.put(Constants.BODY_PART_WATER_VALUE, String.valueOf(Math.ceil(currentWeekAvgFaceValue * 1000) / 10f));
//			faceMap.put(Constants.BODY_PART_CONDITION_VALUE, getString(R.string.skin_health4normal));
        recordPartList.add(faceMap);
        HashMap<String, String> eyeMap = new HashMap<String, String>();
        float preWeekAvgEyeValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_EYE), userId, String.valueOf(preMonthStartTime.getTime()), String.valueOf(mediumTime.getTime()));
        float currentWeekAvgEyeValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_EYE), userId, String.valueOf(mediumTime.getTime()), String.valueOf(endTime.getTime()));
        eyeMap.put(Constants.BODY_PART_TYPE, String.valueOf(Constants.BODY_PART_EYE));
        eyeMap.put(Constants.BODY_PART_COMPARE_LABEL, getString(R.string.skin_healthLabel4comparsion_week));
        if (preWeekAvgEyeValue == 0.0f) {
            eyeMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentWeekAvgEyeValue - preWeekAvgEyeValue) / 0.01f * 10) / 10f + "%"));
        } else {
            eyeMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentWeekAvgEyeValue - preWeekAvgEyeValue) / preWeekAvgEyeValue * 10) / 10f + "%"));
        }

//			eyeMap.put(Constants.BODY_PART_COMPARE_PROCESS, "20");
        eyeMap.put(Constants.BODY_PART_WATER_VALUE, String.valueOf(Math.ceil(currentWeekAvgEyeValue * 1000) / 10f));
//			eyeMap.put(Constants.BODY_PART_CONDITION_VALUE, getString(R.string.skin_health4normal));
        recordPartList.add(eyeMap);
//			HashMap<String,String> neckMap = new HashMap<String, String>();
//			float preWeekAvgNeckValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_NECK),userId,String.valueOf(preMonthStartTime.getTime()),String.valueOf(mediumTime.getTime()));
//			float currentWeekAvgNeckValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_NECK),userId,String.valueOf(mediumTime.getTime()),String.valueOf(endTime.getTime()));
//			neckMap.put(Constants.BODY_PART_TYPE, String.valueOf(Constants.BODY_PART_NECK));
//			neckMap.put(Constants.BODY_PART_COMPARE_LABEL,  getString(R.string.skin_healthLabel4comparsion_week));
//			if(preWeekAvgNeckValue == 0.0f){
//				neckMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentWeekAvgNeckValue - preWeekAvgNeckValue)/0.01f * 10)/10f + "%"));
//			}else{
//				neckMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentWeekAvgNeckValue - preWeekAvgNeckValue)/preWeekAvgNeckValue * 10)/10f + "%"));
//			}
//			
////			neckMap.put(Constants.BODY_PART_COMPARE_PROCESS, "20");
//			neckMap.put(Constants.BODY_PART_WATER_VALUE, String.valueOf(Math.ceil(currentWeekAvgNeckValue * 1000)/10f));
////			neckMap.put(Constants.BODY_PART_CONDITION_VALUE, getString(R.string.skin_health4normal));
//			recordPartList.add(neckMap);
        HashMap<String, String> handMap = new HashMap<String, String>();
        float preWeekAvgHandValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_HAND), userId, String.valueOf(preMonthStartTime.getTime()), String.valueOf(mediumTime.getTime()));
        float currentWeekAvgHandValue = getAverageByTimeCategory(String.valueOf(Constants.BODY_PART_HAND), userId, String.valueOf(mediumTime.getTime()), String.valueOf(endTime.getTime()));
        handMap.put(Constants.BODY_PART_TYPE, String.valueOf(Constants.BODY_PART_HAND));
        handMap.put(Constants.BODY_PART_COMPARE_LABEL, getString(R.string.skin_healthLabel4comparsion_week));
        if (preWeekAvgHandValue == 0.0f) {
            handMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentWeekAvgHandValue - preWeekAvgHandValue) / 0.01f * 10) / 10f + "%"));
        } else {
            handMap.put(Constants.BODY_PART_COMPARE_VALUE, String.valueOf(Math.ceil((currentWeekAvgHandValue - preWeekAvgHandValue) / preWeekAvgHandValue * 10) / 10f + "%"));
        }

//			handMap.put(Constants.BODY_PART_COMPARE_PROCESS, "20");
        handMap.put(Constants.BODY_PART_WATER_VALUE, String.valueOf(Math.ceil(currentWeekAvgHandValue * 1000) / 10f));
//			handMap.put(Constants.BODY_PART_CONDITION_VALUE, getString(R.string.skin_health4normal));
        recordPartList.add(handMap);
        recordPartAdapter.setRecordPartList(recordPartList);
        recordListView.setAdapter(recordPartAdapter);
    }

    private float getAverageByTimeCategory(String partType, String userId, String startTime, String endTime) {
        float averageValue = 0.0f;
        List<DetectModel> detectModelList = detectDao.queryRaw("WHERE PART_TYPE = ? AND USER_ID = ? AND INSERT_TIME > ? AND INSERT_TIME < ? AND DETECT_TYPE = ?",
                new String[]{partType, userId, startTime, endTime, String.valueOf(Constants.DETECT_TYPE_WATER)});
        if (detectModelList != null && detectModelList.size() > 0) {
            float sumValue = 0.0f;
            for (DetectModel detectModel : detectModelList) {
                sumValue += (float) (detectModel.getDetectValue() * 0.1f);
            }
            averageValue = sumValue / detectModelList.size();
        }
        return averageValue;
    }


    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MainScreen"); //统计页面
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MainScreen");
    }


    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    protected void dialog(String msg, final HashMap<String, String> typeMap, final int timeType) {
        AlertDialog.Builder builder = new Builder(getActivity());
        builder.setMessage(msg);
        builder.setPositiveButton(mContext.getResources().getString(R.string.detect_goto_detect), new AlertDialog.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                specCallback.switchSpecViewById(SpecActivity.SPEC_DETECT_VIEW_ID);
            }

        });

        builder.setNegativeButton(mContext.getResources().getString(R.string.detect_goto_detail), new AlertDialog.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                startActivity(typeMap, timeType);
            }
        });

        builder.create().show();
    }

    private void startActivity(HashMap<String, String> typeMap, int timeType) {
        Intent intent = new Intent(mContext, SpecRecordPartLinerDetailActivity.class);
        intent.putExtra("PartType", typeMap);
        intent.putExtra("TimeType", timeType);
        mContext.startActivity(intent);
    }


}
